package com.x.processplatform.assemble.surface.jaxrs.read;

import java.util.List;

import org.apache.commons.lang3.BooleanUtils;

import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.Applications;
import com.x.base.core.project.x_processplatform_service_processing;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.assemble.surface.Control;
import com.x.processplatform.assemble.surface.ThisApplication;
import com.x.processplatform.assemble.surface.WorkControlBuilder;
import com.x.processplatform.core.entity.content.Work;
import com.x.processplatform.core.express.service.processing.jaxrs.read.ActionCreateWithWorkWi;

import io.swagger.v3.oas.annotations.media.Schema;

class ActionCreateWithWork extends BaseAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(ActionCreateWithWork.class);

	ActionResult<List<Wo>> execute(EffectivePerson effectivePerson, String workId, JsonElement jsonElement)
			throws Exception {

		LOGGER.debug("execute:{}, workId:{}.", effectivePerson::getDistinguishedName, () -> workId);

		ActionResult<List<Wo>> result = new ActionResult<>();
		Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
		Work work = null;
		if (ListTools.isEmpty(wi.getIdentityList())) {
			throw new ExceptionEmptyIdentity();
		}

		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			Business business = new Business(emc);
			work = emc.find(workId, Work.class);
			if (null == work) {
				throw new ExceptionEntityNotExist(workId, Work.class);
			}
			Control control = new WorkControlBuilder(effectivePerson, business, work).enableAllowManage()
					.enableAllowVisit().build();
			if (BooleanUtils.isFalse(control.getAllowVisit()) && BooleanUtils.isFalse(control.getAllowManage())) {
				throw new ExceptionAccessDenied(effectivePerson, work);
			}
		}

		List<Wo> wos = ThisApplication.context().applications()
				.postQuery(effectivePerson.getDebugger(), x_processplatform_service_processing.class,
						Applications.joinQueryUri("read", "work", work.getId()), wi, work.getJob())
				.getDataAsList(Wo.class);
		result.setData(wos);
		return result;
	}

	@Schema(name = "com.x.processplatform.assemble.surface.jaxrs.read.ActionCreateWithWork$Wi")
	public static class Wi extends ActionCreateWithWorkWi {

		private static final long serialVersionUID = -2119863985965620324L;

	}

	@Schema(name = "com.x.processplatform.assemble.surface.jaxrs.read.ActionCreateWithWork$Wo")
	public static class Wo extends WoId {

		private static final long serialVersionUID = -3580488160517248659L;

	}

}
